package br.consultorio.digital.dao.repositorio;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Cria a base de dados ou atualiza.
 * 
 * @author Paulo
 * 
 */
public class SQLiteHelper extends SQLiteOpenHelper {

	private String[] insertScript;
	private String[] scriptSQLCreate;

	public SQLiteHelper(Context context, String nomeBanco, int versaoBanco,
			String[] scriptSQLCreate, String[] insertScritp) {

		super(context, nomeBanco, null, versaoBanco);
		this.scriptSQLCreate = scriptSQLCreate;
		this.insertScript = insertScritp;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// Cria��o das tabelas do DB.
		for (String script : scriptSQLCreate) {
			db.execSQL(script);
		}

		// Insers�o dos registros principais.
		for (String insert : insertScript) {
			db.execSQL(insert);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int versaoAntiga, int versaoNova) {
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
		if (!db.isReadOnly()) {
			// Enable foreign key constraints
			db.execSQL("PRAGMA foreign_keys=ON;");
		}
	}

}